package com.jc.oms.controller.user;

import com.jc.common.util.CheckNullUtils;
import com.jc.common.util.WebUtils;
import com.jc.ds.mapper.UserMapper;
import com.jc.ds.model.User;
import com.jc.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;


    /**
     * 新增用户
     */
    @PostMapping("userAdd")
    public Map<String, Object> userAdd(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = new HashMap<>();

        String userName = null;
        String passWord = null;
        String name = null;
        try {
            map = WebUtils.ReadAsChars(request);
            userName = String.valueOf(map.get("userName"));
            passWord = String.valueOf(map.get("passWord"));
            name = String.valueOf(map.get("name"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        if (CheckNullUtils.isNull(userName)) {
            result.put("msg", "用户名为空!");
            return result;
        }
        if (CheckNullUtils.isNull(passWord)) {
            result.put("msg", "密码为空!");
            return result;
        }
        if (CheckNullUtils.isNull(name)) {
            result.put("msg", "真实姓名为空!");
            return result;
        }

        User userQuer = new User();
        userQuer.setUserName(userName);
        Integer integer = this.userMapper.selectAllCount(userQuer);
        if (integer > 0) {

            result.put("msg", "用户名已存在!");
            result.put("code", "5000");

            return result;
        }

        Boolean flag = this.userService.userAdd(userName, passWord, name);

        if (flag) {
            result.put("msg", "注册成功~");
            result.put("code", "10000");
            result.put("data", "ok");
        } else {
            result.put("msg", "系统异常，请稍后再试~");
            result.put("code", "5000");
        }
        return result;
    }

    /**
     * 查询用户
     */
    @PostMapping("userQuery")
    public Map<String, Object> userQuery(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        String userName = null;
        String name = null;
        String start = null;
        String limit = null;
        try {
            params = WebUtils.ReadAsChars(request);
            userName = String.valueOf(params.get("userName"));
            name = String.valueOf(params.get("name"));
            start = String.valueOf(params.get("start"));
            limit = String.valueOf(params.get("limit"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        result = this.userService.userQuery(userName, name,start,limit);
        if (result == null) {
            result.put("msg", "没有用户~~");
        }

        return result;
    }

    /**
     * 查询用户
     */
    @PostMapping("userQueryByName")
    public Map<String, Object> userQueryByName(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        String name = null;
        try {
            params = WebUtils.ReadAsChars(request);
            name = String.valueOf(params.get("name"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        result = this.userService.userQueryByName(name);
        if (result == null) {
            result.put("msg", "没有用户~~");
            result.put("data", "没有用户~~");
            result.put("code", 7777);
        }

        return result;
    }

    /***
     *
     *修改用户
     */

    @PostMapping("userUpdate")
    public Map<String, Object> userUpdate(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();

        try {
            params = WebUtils.ReadAsChars(request);
            String userName = String.valueOf(params.get("userName"));
            String passWord = String.valueOf(params.get("passWord"));
            String name = String.valueOf(params.get("name"));
            String id = String.valueOf(params.get("id"));


            int i = this.userService.userUpdate(id, userName, name, passWord);

            if (i == 1) {
                result.put("msg", "修改成功！");
                result.put("code", "10000");
                result.put("data", "修改成功");
            } else {
                result.put("msg", "修改失败！");
                result.put("code", "5000");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return result;
    }

    /***
     * 删除用户
     */
    @PostMapping("userDelete")
    public Map<String, Object> userDelete(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        int id = 0;
        try {
            params = WebUtils.ReadAsChars(request);
            id = Integer.valueOf(String.valueOf(params.get("id")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        int i = this.userService.userDelete(id);

        if (i == 1) {
            result.put("msg", "删除成功！");
            result.put("code", "10000");
        } else {
            result.put("msg", "删除失败！");
            result.put("code", "5000");
        }

        return result;
    }

    /***
     * 前台模糊匹配userName所需
     * 所有用户名
     */

    @PostMapping("selectAllUserName")
    public Map<String, Object> selectAllUserName() {

        Map<String, Object> result = new HashMap<>();

        result = this.userService.selectAllUserName();

        return result;

    }

    /***
     * 前台模糊匹配name所需
     * 所有用户名
     */

    @PostMapping("selectAllName")
    public Map<String, Object> selectAllName() {

        Map<String, Object> result = new HashMap<>();

        result = this.userService.selectAllName();

        return result;

    }

    /***
     *根据用户id查询用户
     */
    @PostMapping("queryUserById")
    public Map<String, Object> queryUserById(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();

        try {
            params = WebUtils.ReadAsChars(request);

            if (params == null) {
                result.put("msg", "收到参数为空");
                result.put("code", "4000");
                return result;
            }

            result = this.userService.queryUserById(params);


        } catch (UnsupportedEncodingException e) {
            result.put("msg", "系统出错~");
            result.put("code", "4000");
            e.printStackTrace();
        }
        return result;
    }

    /***
     *根据用户ids查询用户
     */
    @PostMapping("queryUserByIds")
    public Map<String, Object> queryUserByIds(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();

        try {
            params = WebUtils.ReadAsChars(request);

            if (params == null) {
                result.put("msg", "收到参数为空");
                result.put("code", "4000");
                return result;
            }

            result = this.userService.queryUserByIds(params);


        } catch (UnsupportedEncodingException e) {
            result.put("msg", "系统出错~");
            result.put("code", "4000");
            e.printStackTrace();
        }
        return result;
    }


    /***
     *用户到角色
     */
    @PostMapping("userToRole")
    public Map<String, Object> userToRole(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();

        try {
            params = WebUtils.ReadAsChars(request);

            if (params == null) {
                result.put("msg", "收到参数为空");
                result.put("code", "4000");
                return result;
            }
            result = this.userService.userToRole(params);
        } catch (UnsupportedEncodingException e) {
            result.put("msg", "系统出错~");
            result.put("code", "4000");
            e.printStackTrace();
        }
        return result;
    }
}
